# -*- coding: utf-8 -*-
# @Time : 2024/3/4 21:51
# @Author : TH
# Description:实际结果和预期结果比较，并进行处理
class ExpectActualCompare(object):

    def expect_actual_compare(self, expect_value, actual_value, flag):
        """
        调用下面两个方法获取实际和预期的结果
        :param flag: 响应结果是json还是text的标记
        :param expect_value:预期结果
        :param actual_value:实际结果
        :return:返回预期和实际比对的字符串
        """
        if flag == True:
            __result = self.expect_actual_compare_json(expect_value, actual_value)
        else:
            __result = self.expect_actual_compare_text(expect_value, actual_value)
        if not (__result[0]) and not (__result[1]):
            return "预期结果与实际结果一致"
        return "断言失败：\n预期结果:%s\n实际结果:%s" % (__result[0], __result[1])

    def expect_actual_compare_json(self, expect_value, actual_value):
        """
        # 实现预期结果与实际结果的比较(json格式的数据比较)
        :param expect_value:预期结果(dict)
        :param actual_value:实际结果(dict)
        :return: 返回比对后实际和预期的元组
        """
        expect = {}
        actual = {}
        set1 = set(list(expect_value.keys()) + list(actual_value.keys()))
        for key in set1:
            if key not in expect_value.keys():
                actual[key] = actual_value[key]
            elif key not in actual_value.keys():
                expect[key] = expect_value[key]
            else:
                if expect_value[key] != actual_value[key]:
                    expect[key] = expect_value[key]
                    actual[key] = actual_value[key]
        # return "expect:%s\nactual:%s" % (expect, actual)
        return (expect, actual)

    def expect_actual_compare_text(self, expect_value, actual_value):
        """
        # 实现预期结果与实际结果的比较(text格式的数据比较)
        :param expect_value:预期结果(dict)
        :param actual_value:实际结果(dict)
        :return: 返回比对后实际和预期的元组
        """
        get_result = self.expect_actual_compare_json(expect_value, actual_value)
        get_expect = get_result[0]
        get_actual = get_result[1]
        if get_expect["text"] in get_actual["text"]:
            del get_actual["text"]
            del get_expect["text"]
        else:
            del get_actual["text"]
        return (get_expect, get_actual)
